package com.dhecp.project.statistics.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dhecp.common.annotation.Log;
import com.dhecp.common.annotation.RepeatSubmit;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.core.page.TableDataInfo;
import com.dhecp.common.enums.BusinessType;
import com.dhecp.core.utils.ExportExcelUtils;
import com.dhecp.project.common.controller.BaseModuleController;
import com.dhecp.project.statistics.domain.CommonStatistics;
import com.dhecp.project.statistics.service.CommonStatisticsService;
import com.dhecp.project.system.domain.SysDictionaryForExport;
import com.dhecp.project.system.service.SysDictionaryForFieldService;

/**
 * 通用统计分析 控制层
 * @author DH-Xxj
 * @version 2021年2月4日
 */
@Controller
@RequestMapping("/statistics/commonstatistics")
public class CommonStatisticsController extends BaseModuleController{
	String moduleTable = "";
    
    @Autowired
    private CommonStatisticsService commonStatisticsService;
    
    @Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;
    
    /**
     * 打开查询列表页
     * @param moduleTable 必要参数：功能唯一标识
     * @return
     * @author DH-Xxj 
     */
    @GetMapping()
    public ModelAndView listPage(@RequestParam Map<String, Object> params){
		if (params.get("moduleTable") == null || params.get("moduleTable") == ""){
			ModelAndView mv = this.getModelAndView();
	        mv =  this.gotoModelAndViewErrorPage(params, "moduleTable", "功能唯一标识"); //验证必要参数
	        if (!mv.isEmpty()){ //缺少必要参数时返回错误提示页面
	        	return mv;
	        }
		}
    	params.put("entity", new CommonStatistics());
    	params.put("prefix","statistics/commonstatistics"); //页面路径
    	
    	ModelAndView mv = this.openListPage(params);
    	if (params.get("v10") != null){mv.addObject("v10", params.get("v10"));}	//备用查询条件字段
    	if (params.get("v11") != null){mv.addObject("v11", params.get("v11"));}	//备用查询条件字段
    	if (params.get("v12") != null){mv.addObject("v12", params.get("v12"));}	//备用查询条件字段
    	if (params.get("v13") != null){mv.addObject("v13", params.get("v13"));}	//备用查询条件字段
    	if (params.get("v14") != null){mv.addObject("v14", params.get("v14"));}	//备用查询条件字段
    	if (params.get("v15") != null){mv.addObject("v15", params.get("v15"));}	//备用查询条件字段
		return mv;
    }
	
	/**
	 * 查询数据
	 * @param moduleTable 必要参数：功能唯一标识
	 * @param v10、v11、v12、v13、v14、v15 备用查询条件字段
	 * @return
	 * @author DH-Xxj
	 */
	@GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map<String, Object> params){
		if (params.get("moduleTable") == null || params.get("moduleTable") == ""){
			return getDataTable("缺少功能唯一标识【moduleTable】");
		}else{
			moduleTable = params.get("moduleTable").toString();
		}
        List<CommonStatistics> list = new ArrayList<CommonStatistics>();
        try {
        	if ("VP_STOCKBARGAININSKQKFX".equals(moduleTable.toUpperCase())){
        		//查询销售合同收款情况分析（出库单弹窗分析）
            	list = commonStatisticsService.queryStockBargainInSkQkFx(params);
        	}
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(this.getExceptionMessage(e).toString());            
        }
        return getDataTable(list);
    }
	
	/**
     * 数据导出Excel
     * @param params
     * @return
     * @throws Exception
     * @author DH-Xxj
     */
    @Log(title = "数据分析导出", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult export(@RequestParam Map<String, Object> params) throws Exception {
		if (params.get("moduleTable") == null || params.get("moduleTable") == ""){
			moduleTable = "moduleTable";
		}else{
			moduleTable = params.get("moduleTable").toString();
		}
		String exportName = "数据分析导出";
    	params.put("entity", new CommonStatistics()); 
        List<CommonStatistics> list = new ArrayList<CommonStatistics>();
    	if ("VP_STOCKBARGAININSKQKFX".equals(moduleTable.toUpperCase())){
    		//查询销售合同收款情况分析（出库单弹窗分析）
        	list = commonStatisticsService.queryStockBargainInSkQkFx(params);
        	exportName = "销售合同收款情况";
    	}
        params.put("getFieldAttributesType", "export");
    	List<SysDictionaryForExport> fieldList = dictionaryForFieldService.queryExportFieldList(params);
        ExportExcelUtils<CommonStatistics> util = new ExportExcelUtils<CommonStatistics>(moduleTable,new CommonStatistics());
        return util.exportExcel(list, fieldList, exportName, "export");
    }
    
}
